package com.zmn.brs.admin.controller.blacklist;

import com.zmn.brs.model.entity.blacklist.Blacklist;
import com.zmn.brs.model.entity.blacklist.query.BlacklistQuery;
import com.zmn.brs.services.interfaces.blacklist.BlacklistService;
import com.zmn.brs.services.interfaces.whitelist.WhitelistService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 描述: 黑名单页面控制类
 *
 * @author wangjie
 * @since 2019-07-31
 **/
@Controller
@RequestMapping(value = "/blacklist/")
public class BlacklistListController extends BlacklistBaseController {

    public BlacklistListController(BlacklistService blacklistService, WhitelistService whitelistService) {
        super(blacklistService, whitelistService);
    }

    /**
     * 描述: 黑名单列表跳转，加载方法
     *
     * @param query
     *            查询条件
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping(value = "list")
    public ModelAndView list(@ModelAttribute BlacklistQuery query) {

        ModelAndView mv = new ModelAndView("blacklist/list");
        List<Blacklist> blacklistArr = Optional.ofNullable(blacklistService.listPageByQuery(query)).orElse(Collections.emptyList());
        mv.addObject("items", blacklistArr);
        mv.addObject("vo", query);
        mv.addObject("blacklistTypeList", getBlacklistTypeList());
        mv.addObject("title", getBlacklistTypeTitleByTypeNum(query.getBlacklistType()));
        return mv;
    }

    /**
     * 描述: 添加黑名单IP页面跳转
     *
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping(value = "add_blacklist")
    // @RequiresPermissions(value = "/blacklist/add_blacklist.action")
    public ModelAndView add(@RequestParam(value = "blacklistType", defaultValue = "0") Integer blacklistType) {
        ModelAndView mv = new ModelAndView("blacklist/add");
        mv.addObject("blacklistType", blacklistType);
        mv.addObject("title", getBlacklistTypeTitleByTypeNum(blacklistType));
        return mv;
    }
}